/*
 *  (C) Copyright 2007 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Jazz.
 *
 *  Jazz is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Jazz is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.jazz.binding;

import java.io.Serializable;
import java.lang.reflect.Field;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Default {@link BindingAttribute} implementation.
 * 
 * @author yanaga
 * @since 1.0
 * @see BindingAttribute
 * @see BindingAnnotationBeanPostProcessor
 */
public class DefaultBindingAttribute implements BindingAttribute, Serializable {

	private static final long serialVersionUID = 1L;

	protected final Log logger = LogFactory.getLog(getClass());

	private Object view;

	private Field field;

	private Object bean;

	private String propertyName;

	public DefaultBindingAttribute(Object view, Field field, Object model, String propertyName) {
		this.view = view;
		this.field = field;
		this.bean = model;
		this.propertyName = propertyName;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append("[");
		sb.append(field);
		sb.append(",");
		sb.append(bean);
		sb.append(",");
		sb.append(propertyName);
		sb.append("]");

		return sb.toString();
	}

	@Override
	public Object getField() {
		try {
			this.field.setAccessible(true);
			return this.field.get(view);
		}
		catch (Exception ex) {
			throw new IllegalArgumentException(ex);
		}
	}

	@Override
	public Object getBean() {
		return this.bean;
	}

	@Override
	public String getPropertyName() {
		return this.propertyName;
	}

}
